﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrimaryAlgorithm.Reverse
{
    class MainProject
    {
        static void Main(string[] args)
        {
            ////char[] s = new char[] { 'h', 'e', 'l', 'l', 'o' };
            int s = -1236546139;
            Console.WriteLine(Reverse(s));
            Console.WriteLine();
            Console.ReadKey();
        }
        public static int Reverse(int x)
        {
            long res = 0;
            while (x!=0)
            {
                res = res * 10 + x % 10;
                x /= 10;
            }
            return (int)res == res ? (int)res : 0;
        }
        public static int Reverse1(int x)
        {
            if (x == 0) return 0;
            string flag = string.Empty;
            string xStr = x.ToString();
            if (xStr[0] == '-')
            {
                flag += "-";
                xStr = xStr.Substring(1, xStr.Length-1);
            }
            

            char[] xStrs = xStr.ToArray<char>();
            Array.Reverse(xStrs);
            xStr = string.Empty;
            xStr += flag;
            int index = 0;
            for (int i = 0; i < xStrs.Length; i++)
            {
                if (xStrs[index] == '0')
                {
                    index++;
                    continue;
                }
                xStr += xStrs[i];
            }
            try
            {
                return int.Parse(xStr);
            }
            catch (Exception)
            {
                return 0;
            }
        }
    }
}
